<?php
class Online_Product_Info extends QDB_ActiveRecord_Abstract
{
	/**
	 * 返回对象的定义
	 *
	 * @static
	 *
	 * @return array
	 */
	static function __define()
	{
		return array
		(
			// 指定该 ActiveRecord 要使用的行为插件
			'behaviors' => '',

			// 指定行为插件的配置
			'behaviors_settings' => array
			(
			    # '插件名' => array('选项' => 设置),
			),

			// 用什么数据表保存对象
			'table_name' => 'product_info',

			// 指定数据表记录字段与对象属性之间的映射关系
			// 没有在此处指定的属性，QeePHP 会自动设置将属性映射为对象的可读写属性
			'props' => array
			(
				// 主键应该是只读，确保领域对象的“不变量”
				'id' => array('readonly' => true),

				/**
				 *  可以在此添加其他属性的设置
				 */
				# 'other_prop' => array('readonly' => true),

				/**
				 * 添加对象间的关联
				 */
				# 'other' => array('has_one' => 'Class'),

			),

			/**
			 * 允许使用 mass-assignment 方式赋值的属性
			 *
			 * 如果指定了 attr_accessible，则忽略 attr_protected 的设置。
			 */
			'attr_accessible' => '',

			/**
			 * 拒绝使用 mass-assignment 方式赋值的属性
			 */
			'attr_protected' => 'id',

			/**
			 * 指定在数据库中创建对象时，哪些属性的值不允许由外部提供
			 *
			 * 这里指定的属性会在创建记录时被过滤掉，从而让数据库自行填充值。
			 */
			'create_reject' => '',

			/**
			 * 指定更新数据库中的对象时，哪些属性的值不允许由外部提供
			 */
			'update_reject' => '',

			/**
			 * 指定在数据库中创建对象时，哪些属性的值由下面指定的内容进行覆盖
			 *
			 * 如果填充值为 self::AUTOFILL_TIMESTAMP 或 self::AUTOFILL_DATETIME，
			 * 则会根据属性的类型来自动填充当前时间（整数或字符串）。
			 *
			 * 如果填充值为一个数组，则假定为 callback 方法。
			 */
			'create_autofill' => array
			(
				# 属性名 => 填充值
				# 'is_locked' => 0,
			),

			/**
			 * 指定更新数据库中的对象时，哪些属性的值由下面指定的内容进行覆盖
			 *
			 * 填充值的指定规则同 create_autofill
			 */
			'update_autofill' => array
			(
			),

			/**
			 * 在保存对象时，会按照下面指定的验证规则进行验证。验证失败会抛出异常。
			 *
			 * 除了在保存时自动验证，还可以通过对象的 ::meta()->validate() 方法对数组数据进行验证。
			 *
			 * 如果需要添加一个自定义验证，应该写成
			 *
			 * 'title' => array(
			 *	array(array(__CLASS__, 'checkTitle'), '标题不能为空'),
			 * )
			 *
			 * 然后在该类中添加 checkTitle() 方法。函数原型如下：
			 *
			 * static function checkTitle($title)
			 *
			 * 该方法返回 true 表示通过验证。
			 */
			'validations' => array
			(
			),
		);
	}


/* ------------------ 以下是自动生成的代码，不能修改 ------------------ */

	/**
	 * 开启一个查询，查找符合条件的对象或对象集合
	 *
	 * @static
	 *
	 * @return QDB_Select
	 */
	static function find()
	{
		$args = func_get_args();
		return QDB_ActiveRecord_Meta::instance(__CLASS__)->findByArgs($args);
	}

	/**
	 * 返回当前 ActiveRecord 类的元数据对象
	 *
	 * @static
	 *
	 * @return QDB_ActiveRecord_Meta
	 */
	static function meta()
	{
		return QDB_ActiveRecord_Meta::instance(__CLASS__);
	}


/* ------------------ 以上是自动生成的代码，不能修改 ------------------ */
	
	static function create($product_info)
	{
		$info = Online_Product_Info::find('product_basic_id=? and lang=?', $product_info['product_basic_id'], $product_info['lang'])->asArray()->getOne();
		if(empty($info))
		{
			$create = new Online_Product_Info();
			$create->product_basic_id = $product_info['product_basic_id'];
			$create->lang = $product_info['lang'];
			$create->title = $product_info['title'];
			$create->description = $product_info['description'];
			$create->last_edit_user_id = CURRENT_USER_ID;
			$create->last_edit_time = CURRENT_DATETIME;
			$create->save();
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => '产品已存在该语种描述,请检查填写的产品资料!');
		}
	}
	
	static function edit($edit_info)
	{
		$info = Online_Product_Info::find('id=?', $edit_info['id'])->asArray()->getOne();
		if ($info)
		{
			$product_info = Online_Product_Info::find('id=?', $edit_info['id'])->getOne();
			$product_info->title = $edit_info['title'];
			$product_info->description = $edit_info['description'];
			$product_info->last_edit_time = CURRENT_DATETIME;
			$product_info->last_edit_user_id = CURRENT_USER_ID;
			$product_info->save();
			return array('ack' => SUCCESS, 'message' => '');
		}
		else
		{
			return array('ack' => FAILURE, 'message' => '没有确定产品id!');
		}
	}
	
	static function searchDistinct($condition, $cp = 0, $ps = 0)
	{
		set_time_limit(0);
		$where = Online_Product_Info::buildSearchWhereDistinct($condition);
		if ($cp == 0 && $ps == 0)
		{
			$select = Product_Basic::find($where)
				->joinLeft('product_info', '', 'product_info.product_basic_id=product_basic.id')
				->distinct('product_info.product_basic_id');
			$page = array();
		}
		else
		{
			$select = Product_Basic::find($where)
				->joinLeft('product_info', '', 'product_info.product_basic_id=product_basic.id')
				->distinct('product_info.product_basic_id')->limitPage($cp, $ps);
			$page = $select->getPagination();
		}	
		$product_basic_id = $select->asArray()->getAll();
		return array(
			'ack' => SUCCESS,
			'message' => '',
			'data' => $product_basic_id,
			'pagination' => $page
		);
	}
	
	static function buildSearchWhereDistinct($condition)
	{
		$where = '1';
		if ( ! empty($condition['sku']))
		{
			$where .= ' and number in(' . $condition['sku'] . ')';
		}
		if ( ! empty($condition['product_categories_id']))
		{
			$categories = Product_Categories::getOffspringById($condition['product_categories_id']);
			array_push($categories, $condition['product_categories_id']);
			$where .= ' and product_categories_id in(' . implode(',', $categories) . ')';
		}
		if ( ! empty($condition['lang']))
		{
			$where .= ' and product_info.lang in (' . implode(',', $condition['lang']) . ')';
		}
		if ( ! empty($condition['no_lang']))
		{
			$where .= ' and (product_info.product_basic_id not in (select product_info.product_basic_id from product_info where lang in (' . implode(',', $condition['no_lang']) . ')))';
		}
		return $where;
	}
	
	static function import($product_info, $type = '')
	{
		$language_message = '';
		$product_message = '';
		foreach ($product_info as $pi)
		{
			$language = Helper_BSS_Normal::getIniOne('online_ebay_product_language', 'text', $pi['Language'], 'value');
			if ($language)
			{
				$product_basic_number = str_pad($pi['SKU'], '6', '0', STR_PAD_LEFT);
				$product_basic_number = substr($product_basic_number, 0, 6);
				$product_basic = Product_Basic::find('number=?', $product_basic_number)->asArray()->getOne();
				if ($product_basic)
				{
					$result = Online_Product_Info::find('product_basic_id=? and lang=?', $product_basic['id'], $language)->asArray()->getOne();
					if ($result)
					{
						$online_product_info = Online_Product_Info::find('id=?', $result['id'])->getOne();
					}
					else
					{
						$online_product_info = new Online_Product_Info();
						$online_product_info->product_basic_id = $product_basic['id'];
						$online_product_info->lang = $language;
					}
					if ($type != 'only_insert' || ! $result)
					{
						$online_product_info->title = $pi['Title'];
						$online_product_info->description = $pi['Description'];
						$online_product_info->last_edit_time = CURRENT_DATETIME;
						$online_product_info->last_edit_user_id = CURRENT_USER_ID ? CURRENT_USER_ID : 0;
						$online_product_info->save();
					}
				}
				else
				{
					$product_message = "部分产品不存在;";
				}
			}
			else
			{
				$language_message = "部分产品语种不正确;";
			}
		}
		if ($language_message || $product_message)
		{
			return array('ack' => FAILURE, 'message' => $product_message . $language_message);
		}
		else
		{
			return array('ack' => SUCCESS, 'message' => '');
		}
	}
	
	static function search($condition, $cp = 0, $ps = 0)
	{
		$where = Online_Product_Info::buildSearchWhere($condition);
		if ($cp == 0 && $ps == 0)
		{
			$select = Online_Product_Info::find($where)
				->joinLeft('product_basic', 'number', 'product_basic.id=product_info.product_basic_id');
			$page = array();
		}
		else
		{
			$select = Online_Product_Info::find($where)
				->joinLeft('product_basic', 'number', 'product_basic.id=product_info.product_basic_id')
				->limitPage($cp, $ps);
			$page = $select->getPagination();
		}	
		$product_basic_id = $select->asArray()->getAll();
		return array(
			'ack' => SUCCESS,
			'message' => '',
			'data' => $product_basic_id,
			'pagination' => $page
		);
	}
	
	static function buildSearchWhere($condition)
	{
		$where = '1';
		if ( ! empty($condition['sku']))
		{
			$where .= ' and number in(' . $condition['sku'] . ')';
		}
		if ( ! empty($condition['product_categories_id']))
		{
			$categories = Product_Categories::getOffspringById($condition['product_categories_id']);
			array_push($categories, $condition['product_categories_id']);
			$where .= ' and product_categories_id in(' . implode(',', $categories) . ')';
		}
		if ( ! empty($condition['lang']))
		{
			$where .= ' and product_info.lang in (' . implode(',', $condition['lang']) . ')';
		}
		return $where;
	}
}